#coding:utf-8

import json
from criet import prpcrypt
import gconf
from sqlhelp import sql_action
key = '(1(0#5Hb@sa&echo'
def get_pwds():
    try:
        sql = "select * from password"
        sql_help = sql_action()
        aa = sql_help._sql_selectall(sql)
        return aa
    except:
        return []

def get_tagpwds(list):
    try:
        sql = "select * from password where tag_id= %s"
        sql_help = sql_action()
        aa = sql_help._sql_selectall(sql,list)
        return aa
    except:
        return []



def validate_login(password):
    sql = "select password from password where tag_id='root' and title ='root'"
    sql_help = sql_action()
    root_pwd = sql_help._sql_selectall(sql)
    #print root_pwd
    pc = prpcrypt(key)
    if pc.decrypt(root_pwd[0][0]) == password:
        return True
    else:
        return False

#def check_tag(tag):

def add_pwd(list):
    sql = "insert into password (tag_id,title,accounts,password,telphone,email) VALUES (%s,%s,%s,%s,%s,%s)"
    sql_help = sql_action()
    sql_help._sql_change(sql,list)
    return True

def add_tag(list):
    sql = "insert into tags (tag_name) values (%s)"
    sql_help = sql_action()
    sql_help._sql_change(sql,list)
    return True

def get_onepwd(id):
    sql = "select tag_id,title,accounts,password,telphone,email from password where id= %s"
    sql_help = sql_action()
    return sql_help._sql_selectone(sql,id)



def update_pwd(list):
    sql = "update password set tag_id=%s,title=%s,accounts=%s,password=%s,telphone=%s,email=%s where id= %s"
    print list
    sql_help = sql_action()
    sql_help._sql_change(sql,list)
    return True

def update_pwd_tag(list):
    sql = "update password set tag_id=%s where tag_id=%s"
    sql_help = sql_action()
    sql_help._sql_change(sql,list)
    return True

def update_tag(list):
    sql = "update tags set tag_name=%s where tag_id=%s"
    sql_help = sql_action()
    sql_help._sql_change(sql, list)
    return True

def delete_pwd(list):
    sql = "delete from password where id=%s"
    sql_help = sql_action()
    sql_help._sql_change(sql,list)
    return True

def delete_tag(list):
    sql = "delete from tags where tag_id=%s"
    sql_help = sql_action()
    sql_help._sql_change(sql, list)
    return True

def get_tags():
    sql = "select * from tags"
    sql_help = sql_action()
    data=sql_help._sql_selectall(sql)
    print data
    return data

def get_tag(list):
    try:
        sql = "select * from tags where tag_name=%s"
        sql_help = sql_action()
        return sql_help._sql_selectone(sql,list)
    except:
        return False

def get_tag_name(list):
    sql = "select tag_name from tags where tag_id=%s"
    sql_help = sql_action()
    return sql_help._sql_selectone(sql,list)